/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package GUI;

import flightlifter.Draw;
import java.awt.event.MouseEvent;
import java.util.*;
import org.lwjgl.opengl.GL11;
import org.lwjgl.opengl.GL20;

/**
 * Vertical Layouts stack widgets vertically. They enforce a uniform width,
 * but allow individual items within the stack to vary in height.
 */
public class VerticalLayout extends GUILayout {

    public int titleHeight;
    public String title = "title";
    int rollup = 0;

    /**
     * @param parent
     * @param x
     * @param y
     * @param width
     */
    public VerticalLayout(GUI parent, int x, int y, int width, String title) {
        setParent(parent);
        this.title = title;
        locX = x;
        locY = y;
        cWidth = width;
        titleHeight = parent.font.getHeight() + 3;
        
    }
    /**
     * Sets the positions of all the subcomponents.
     * Should not be called by the user.
     */
    protected void doLayout() {
        GUIComponent temp;
        int yOff = locY - titleHeight;
        cHeight = titleHeight;

        //only render components if the component is unrolled
        if (rollup == 0) {
            for (int i = 0; i < guiComponents.size(); i++) {

                temp = (GUIComponent) guiComponents.elementAt(i);
                
                temp.setSize(cWidth, parent.font.getHeight() + 3);
                yOff -= temp.cHeight;
                cHeight += temp.cHeight;
                temp.setPosition(locX, yOff);
            }
        }

    }

    /**
     * Render the layout. Should not be called by the user.
     *
     */
    public void draw() {
        GUIComponent temp;

        if (visible == 1) {
            
            //draw the header box
            if (titleHeight > 1) {
                GL11.glColor4f(parent.TITLE_FILL[0],parent.TITLE_FILL[1],parent.TITLE_FILL[2], parent.TITLE_FILL[3]);
                Draw.fillRect(locX, locY, cWidth + 1, -titleHeight);
                
                if (title != null) {
                    GL11.glColor4f(parent.TITLE_TEXT[0],parent.TITLE_TEXT[1],parent.TITLE_TEXT[2], parent.TITLE_TEXT[3]);
                    parent.font.drawString(locX + 2, locY - titleHeight,title,1,1);
                }
            }

            GL11.glColor4f(parent.BACK[0],parent.BACK[1],parent.BACK[2], parent.BACK[3]);
            Draw.fillRect(locX, locY - cHeight, cWidth + 1, cHeight-titleHeight);

            if (rollup == 0) {
                for (int i = 0; i < guiComponents.size(); i++) {
                    temp = (GUIComponent) guiComponents.elementAt(i);
                    temp.draw();
                }
            }
             
             
        }
    }

    /**
     * Handle mouse presses.
     * @return integer indicating whether the mouse event affected the layout.
     */
    public int mousePressed(int mouseX,int mouseY) {
        
        GUIComponent temp;

        if (visible == 1) {
            //check if they are clicking the title
            if (mouseX < locX + cWidth && mouseX > locX && mouseY < locY  && mouseY > locY - titleHeight) {

                parent.focusedComp = this;

                /*
                if (e.getClickCount() == 2) {
                    rollup = 1 - rollup;
                    doLayout();
                }
                 *
                 */
                return 1;
            }
            if (rollup == 0) {
                for (int i = 0; i < guiComponents.size(); i++) {
                    temp = (GUIComponent) guiComponents.elementAt(i);
                    if (temp.mousePressed(mouseX,mouseY) == 1) {
                        //they've clicked on a component
                        //if it isn't a submenu, hide this menu
                       /*
                        if (isSubMenu == 1 && !(temp instanceof SGMenu)) {
                            setVisible(0);

                        }
                        
                        */
                        return 1;
                    }
                }
            }
        }

        
        return 0;
    }

    /**
     * Handle mouse drags.
     *
     */
    public void mouseDragged(int mouseDX,int mouseDY) {
        
        locX += mouseDX;
        locY += mouseDY;
        
        doLayout();
    }
}
